const qcloud = require('../../vendor/qcloud-weapp-client-sdk/index.js')
const config = require('../../config.js')
const utils = require('../../utils.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    allCLassState: false,
    isBankAdmin:false,
    currentClass: {},
    classInfoDTOs: [],
    noticeDTOs:[],
    curindex:0,
    content:""
  },

  // tab切换
  switchClock(e) {
    this.setData({
      curindex: e.currentTarget.dataset.index
    })
  },
  
  // 全局捕获
  bindViewTap(event) {
    if (event.target.id != 'classId') {
      this.selectComponent("#classId").hideFn()
    }
  },

 


  // 获取班级信息
  getobtainClassInfos() {
    utils.ajaxFn({
      url: config.service.obtainClassInfos,
      callback: (data) => {
        if (data.isBankAdmin) {
          let obj = {
            name: '全体',
            ciId: 0,
            groups:[]
          }

          data.classInfoDTOs.splice(0, 0, obj)
        }

        this.setData({
          classInfoDTOs: data.classInfoDTOs.filter(value => !!value.isAdmin)
        })

        var classlist = {
          name: this.data.classInfoDTOs[0].name,
          ciId: this.data.classInfoDTOs[0].ciId,
          groups: this.data.classInfoDTOs[0].groups,
          isMember: this.data.classInfoDTOs[0].isMember
        }

        this.setData({
          currentClass: classlist
        })

        this.obtainNoticesUnreaded()
      }
    })
  },

  // 检测输入公告
  bindKeyInput(e) {
    this.setData({
      content: e.detail.value
    })
  },



  // 班级切换操作
  classSwitch(e) {
    this.setData({
      allCLassState: !this.data.allCLassState
    })
  },

  // 班级切换操作
  switchClass(value) {
    let curData = value.detail.currentClass
    var classlist = {
      name: curData.name,
      ciId: curData.ciId,
      groups: curData.groups,
      isMember: curData.isMember
    }

    this.setData({
      currentClass: classlist
    })

    this.setData({
      clockList: []
    })

    this.obtainNoticesUnreaded()
  },

  //发布公告
  addNotice(){
    utils.ajaxFn({
      url: config.service.newNotice,
      body:{
        content: this.data.content,
        ciId: this.data.currentClass['ciId']
      },
      callback: (data) => {
        wx.showModal({
          title: '提示',
          content: "发布成功！"
        })
        
        this.setData({
          content: ''
        })

        // 发布更新历史公告栏
        this.obtainNoticesUnreaded()
      }
    })
  },
  // 查看指定公告
  viewNotice(e) {
    if (this.data.noticeDTOs[e.currentTarget.dataset.index].state) {
      let idatas = this.data.noticeDTOs

      idatas[e.currentTarget.dataset.index].state = false

      this.setData({
        noticeDTOs: idatas
      })

      return false;
    }

    utils.ajaxFn({
      url: config.service.readNotice,
      body:{
        cnId: e.currentTarget.dataset.cnid
      },
      callback: (data) => {
        let datas = this.data.noticeDTOs
        datas.map((value, index) => {
          if (index == e.currentTarget.dataset.index) {
            value.state = true
          }
          return value  
        })

        this.setData({
          noticeDTOs: datas
        })
      }
    })

  },

  // 获取班级公告列表
  obtainNoticesUnreaded() {
    utils.ajaxFn({
      url: config.service.obtainNotices,
      body: {
        ciId: this.data.currentClass['ciId']
      },
      callback: (data) => {

        data.noticeDTOs.map(value=>{
          value.state = false
          value.times = value.createTime.split(' ')[0]
          return value
        })

        this.setData({
          noticeDTOs: data.noticeDTOs
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(){
    this.getobtainClassInfos()
    
  }
 
})